﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_SectionChangeInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 77)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if(!IsPostBack)
        {
            txtSectionChangeTime.Text = System.DateTime.Now.ToShortDateString();
        }
    }
    protected void ddlOldSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddlOldSection.SelectedValue!="0")
        {
            UserManager userManager=new UserManager();
            lbxUserLists.DataSource = userManager.findBySectionId(Convert.ToInt32(ddlOldSection.SelectedValue));
            lbxUserLists.DataTextField = "UserName";
            lbxUserLists.DataValueField = "UserId";
            lbxUserLists.DataBind();
        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList arrayValue = new ArrayList();
        for (int i = 0; i < lbxUserLists.Items.Count; i++)
        {
            if (lbxUserLists.Items[i].Selected)
            {
                lbxChangeUser.Items.Add(new ListItem(lbxUserLists.Items[i].Text, lbxUserLists.Items[i].Value));
                arrayValue.Add(lbxUserLists.Items[i].Value);
            }
        }
        for (int i = 0; i < arrayValue.Count; i++)
        {
            for (int j = 0; j < lbxUserLists.Items.Count; j++)
            {
                if (lbxUserLists.Items[j].Value == arrayValue[i].ToString())
                {
                    lbxUserLists.Items.RemoveAt(j);
                }
            }
        }
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList arrayValue = new ArrayList();
        for (int i = 0; i < lbxChangeUser.Items.Count; i++)
        {
            if (lbxChangeUser.Items[i].Selected)
            {
                lbxUserLists.Items.Add(new ListItem(lbxChangeUser.Items[i].Text, lbxChangeUser.Items[i].Value));
                arrayValue.Add(lbxChangeUser.Items[i].Value);
            }
        }
        for (int i = 0; i < arrayValue.Count; i++)
        {
            for (int j = 0; j < lbxChangeUser.Items.Count; j++)
            {
                if (lbxChangeUser.Items[j].Value == arrayValue[i].ToString())
                {
                    lbxChangeUser.Items.RemoveAt(j);
                }
            }
        }
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < lbxUserLists.Items.Count;i++)
        {
            lbxChangeUser.Items.Add(new ListItem(lbxUserLists.Items[i].Text,lbxUserLists.Items[i].Value));
        }
        lbxUserLists.Items.Clear();
    }
    protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < lbxChangeUser.Items.Count; i++)
        {
            lbxUserLists.Items.Add(new ListItem(lbxChangeUser.Items[i].Text, lbxChangeUser.Items[i].Value));
        }
        lbxChangeUser.Items.Clear();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        bool b = true;
        for (int i = 0; i < lbxChangeUser.Items.Count; i++)
        {
            SectionChangeInfo sectionChangeInfo = new SectionChangeInfo();
            Section newSection = new Section();
            Section oldSection = new Section();
            if (ddlOldSection.SelectedValue != "0")
            {
                oldSection.SectionId = Convert.ToInt32(ddlOldSection.SelectedValue);
            }
            if (ddlNewSection.SelectedValue != "0")
            {
                newSection.SectionId = Convert.ToInt32(ddlNewSection.SelectedValue);
            }
            else
            {
                newSection.SectionId = Convert.ToInt32(ddlOldSection.SelectedValue);
            }
            sectionChangeInfo.OldSection = oldSection;
            sectionChangeInfo.NewSection = newSection;
            if (txtSectionChangeTime.Text != null && txtSectionChangeTime.Text != "")
            {
                sectionChangeInfo.SectionChangeTime = Convert.ToDateTime(txtSectionChangeTime.Text.Trim());
            }
            if (txtChangeCause.Text != null && txtChangeCause.Text != "")
            {
                sectionChangeInfo.ChangeCause = txtChangeCause.Text.Trim();
            }
            else
            {
                sectionChangeInfo.ChangeCause = "";
            }
            TarreOA.TarreOAModels.User user = UserManager.FindByUserId(Convert.ToInt32(lbxChangeUser.Items[i].Value));
            sectionChangeInfo.ChangeUser = user;
            sectionChangeInfo.OldJob = user.Job;
            sectionChangeInfo.OldWage = user.Wage;
            if (ddlNewJob.SelectedValue != "0")
            {
                Job job = new Job();
                job.JobId = Convert.ToInt32(ddlNewJob.SelectedValue);
                sectionChangeInfo.NewJob = job;
            }
            else
            {
                sectionChangeInfo.NewJob = user.Job;
            }
            if (txtNewWage.Text != null && txtNewWage.Text != "")
            {
                sectionChangeInfo.NewWage = Convert.ToDecimal(txtNewWage.Text);
            }
            else
            {
                sectionChangeInfo.NewWage = user.Wage;
            }
            if (!SectionChangeInfoManager.AddSectionChangeInfo(sectionChangeInfo))
            {
                b = false;
            }
        }
        if (b)
        {
            this.lbxChangeUser.Items.Clear();
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('岗位变动成功！')", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('岗位变动失败，请重试！')", true);
        }
    }
}
